package com.yaozheng.vocationaltraining.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yaozheng.vocationaltraining.BuildConfig;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.CarDetailBean;
import com.yaozheng.vocationaltraining.bean.DownPaymentBean;
import com.yaozheng.vocationaltraining.bean.GameResultBean;
import com.yaozheng.vocationaltraining.bean.PKMorePersonPlayerStatusBean;
import com.yaozheng.vocationaltraining.bean.RepaymentReriodBean;
import com.yaozheng.vocationaltraining.dialog.ask.ConfirmCancelAskDialog;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.fragment.BoLiDanDuPoSuiXianFragment;
import com.yaozheng.vocationaltraining.fragment.BoLiDanDuPoSuiXianFragment_;
import com.yaozheng.vocationaltraining.fragment.CheShengHuaHengXianFragment;
import com.yaozheng.vocationaltraining.fragment.CheShengHuaHengXianFragment_;
import com.yaozheng.vocationaltraining.fragment.DiSanZheZeRenXianFragment;
import com.yaozheng.vocationaltraining.fragment.DiSanZheZeRenXianFragment_;
import com.yaozheng.vocationaltraining.fragment.DownPaymentsPercentFragment;
import com.yaozheng.vocationaltraining.fragment.DownPaymentsPercentFragment_;
import com.yaozheng.vocationaltraining.fragment.ExamMenuFragment;
import com.yaozheng.vocationaltraining.fragment.ExamMenuFragment_;
import com.yaozheng.vocationaltraining.fragment.HomeFragment;
import com.yaozheng.vocationaltraining.fragment.HomeFragment_;
import com.yaozheng.vocationaltraining.fragment.IntegralDetailsFragment;
import com.yaozheng.vocationaltraining.fragment.IntegralDetailsFragment_;
import com.yaozheng.vocationaltraining.fragment.IntegralLotteryFragment;
import com.yaozheng.vocationaltraining.fragment.IntegralLotteryFragment_;
import com.yaozheng.vocationaltraining.fragment.IntegralRuleFragment;
import com.yaozheng.vocationaltraining.fragment.IntegralRuleFragment_;
import com.yaozheng.vocationaltraining.fragment.IntegralTypeListFragment;
import com.yaozheng.vocationaltraining.fragment.IntegralTypeListFragment_;
import com.yaozheng.vocationaltraining.fragment.LearnListFragment;
import com.yaozheng.vocationaltraining.fragment.LearnListFragment_;
import com.yaozheng.vocationaltraining.fragment.LearnMenuFragment;
import com.yaozheng.vocationaltraining.fragment.LearnMenuFragment_;
import com.yaozheng.vocationaltraining.fragment.MustPayFragment;
import com.yaozheng.vocationaltraining.fragment.MustPayFragment_;
import com.yaozheng.vocationaltraining.fragment.MyExperienceFragment;
import com.yaozheng.vocationaltraining.fragment.MyExperienceFragment_;
import com.yaozheng.vocationaltraining.fragment.MyExperienceTitleFragment;
import com.yaozheng.vocationaltraining.fragment.MyExperienceTitleFragment_;
import com.yaozheng.vocationaltraining.fragment.MyIntegralFragment;
import com.yaozheng.vocationaltraining.fragment.MyIntegralFragment_;
import com.yaozheng.vocationaltraining.fragment.MyLocusFragment;
import com.yaozheng.vocationaltraining.fragment.MyLocusFragment_;
import com.yaozheng.vocationaltraining.fragment.MyMenuFragment;
import com.yaozheng.vocationaltraining.fragment.MyMenuFragment_;
import com.yaozheng.vocationaltraining.fragment.MyUserInfoFragment;
import com.yaozheng.vocationaltraining.fragment.MyUserInfoFragment_;
import com.yaozheng.vocationaltraining.fragment.NewsMenuFragment;
import com.yaozheng.vocationaltraining.fragment.PKGameCategoryFragment;
import com.yaozheng.vocationaltraining.fragment.PKGameCategoryFragment_;
import com.yaozheng.vocationaltraining.fragment.PaperCatalogFragment;
import com.yaozheng.vocationaltraining.fragment.PaperCatalogFragment_;
import com.yaozheng.vocationaltraining.fragment.PriceFragment;
import com.yaozheng.vocationaltraining.fragment.PriceFragment_;
import com.yaozheng.vocationaltraining.fragment.QuickSearchMenuFragment;
import com.yaozheng.vocationaltraining.fragment.QuickSearchMenuFragment_;
import com.yaozheng.vocationaltraining.fragment.RepaymentPeriodFragment;
import com.yaozheng.vocationaltraining.fragment.RepaymentPeriodFragment_;
import com.yaozheng.vocationaltraining.fragment.SetUpFragment;
import com.yaozheng.vocationaltraining.fragment.SetUpFragment_;
import com.yaozheng.vocationaltraining.fragment.ShangYeBaoXianFragment;
import com.yaozheng.vocationaltraining.fragment.ShangYeBaoXianFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.CheckpointsTripFragment;
import com.yaozheng.vocationaltraining.fragment.exam.CheckpointsTripFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.LimitedChallengeFragment;
import com.yaozheng.vocationaltraining.fragment.exam.LimitedChallengeFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.MasterFragment;
import com.yaozheng.vocationaltraining.fragment.exam.MasterFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.MastersTourFragment;
import com.yaozheng.vocationaltraining.fragment.exam.MastersTourFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.MyExamFragment;
import com.yaozheng.vocationaltraining.fragment.exam.MyExamFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.OnlineExamFragment;
import com.yaozheng.vocationaltraining.fragment.exam.OnlineExamFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKConfirmFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKConfirmFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKHallFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKHallFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKMorePersonConfirmFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKMorePersonConfirmFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKMorePersonHallFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKMorePersonHallFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKRankFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKRankFragment_;
import com.yaozheng.vocationaltraining.fragment.exam.PKResultFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKResultFragment_;
import com.yaozheng.vocationaltraining.fragment.html5.Html5Fragment;
import com.yaozheng.vocationaltraining.fragment.html5.Html5Fragment_;
import com.yaozheng.vocationaltraining.fragment.learn.CourseCatalogFragment;
import com.yaozheng.vocationaltraining.fragment.learn.CourseCatalogFragment_;
import com.yaozheng.vocationaltraining.fragment.learn.ImageListCoursewareDetailFragment;
import com.yaozheng.vocationaltraining.fragment.learn.ImageListCoursewareDetailFragment_;
import com.yaozheng.vocationaltraining.fragment.learn.MyCollectFragment;
import com.yaozheng.vocationaltraining.fragment.learn.MyCollectFragment_;
import com.yaozheng.vocationaltraining.fragment.set_up.AboutFragment;
import com.yaozheng.vocationaltraining.fragment.set_up.AboutFragment_;
import com.yaozheng.vocationaltraining.fragment.set_up.CheckingVersionUpdateFragment;
import com.yaozheng.vocationaltraining.fragment.set_up.CheckingVersionUpdateFragment_;
import com.yaozheng.vocationaltraining.fragment.set_up.SuggestFragment;
import com.yaozheng.vocationaltraining.fragment.set_up.SuggestFragment_;
import com.yaozheng.vocationaltraining.fragment.set_up.VersionFragment;
import com.yaozheng.vocationaltraining.fragment.set_up.VersionFragment_;
import com.yaozheng.vocationaltraining.fragment.userinfo.UpdatePasswordFragment;
import com.yaozheng.vocationaltraining.fragment.userinfo.UpdatePasswordFragment_;
import com.yaozheng.vocationaltraining.fragment.userinfo.UpdateUserInfoFragment;
import com.yaozheng.vocationaltraining.fragment.userinfo.UpdateUserInfoFragment_;
import com.yaozheng.vocationaltraining.iview.set_up.ICheckingVersionView;
import com.yaozheng.vocationaltraining.iview.set_up.IGameRecentView;
import com.yaozheng.vocationaltraining.service.impl.set_up.CheckingVersionServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.set_up.GameRecentServiceImpl;
import com.yaozheng.vocationaltraining.service.set_up.CheckingVersionService;
import com.yaozheng.vocationaltraining.service.set_up.GameRecentService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.PKSaiResultDialog;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 主页activity
 *
 * @author Administrator
 */
@EActivity(R.layout.activity_home)
public class HomeActivity extends BaseActivity implements ICheckingVersionView,IGameRecentView {
    /**
     * 主页碎片
     */
    HomeFragment homeFragment;
    /**
     * 新闻碎片
     */
    NewsMenuFragment newsFragment;
    /**
     * 学习碎片
     */
    LearnMenuFragment learnFragment;
    /**
     * 学习列表碎片
     */
    LearnListFragment learnListFragment;
    /**
     * 考试碎片
     */
    ExamMenuFragment examMenuFragment;
    /**
     * 限时挑战
     */
    LimitedChallengeFragment limitedChallengeFragment;

    /**
     * PK赛
     */
    PKFragment pkFragment;

    /**
     * 排行榜
     */
    PKRankFragment pkRankFragment;

    PKGameCategoryFragment pkGameCategoryFragment;

    PKHallFragment pkHallFragment;
    PKMorePersonHallFragment pkMorePersonHallFragment;

    PKConfirmFragment pkConfirmFragment;
    PKMorePersonConfirmFragment pkMorePersonConfirmFragment;

    MyExamFragment myExamFragment;

    BuyCalculatorActivity buyCalculatorFragment;
    CarTypeDetailActivity carTypeDetailFragment;

    PriceFragment priceFragment;
    MustPayFragment mustPayFragment;
    ShangYeBaoXianFragment shangYeBaoXianFragment;

    DownPaymentsPercentFragment downPaymentsPercentFragment;
    RepaymentPeriodFragment repaymentPeriodFragment;

    DiSanZheZeRenXianFragment diSanZheZeRenXianFragment;
    BoLiDanDuPoSuiXianFragment boLiDanDuPoSuiXianFragment;
    CheShengHuaHengXianFragment cheShengHuaHengXianFragment;

    PKResultFragment pkResultFragment;

    /**
     * 试卷目录碎片
     */
    PaperCatalogFragment paperCatalogFragment;

    /**
     * 我的 碎片
     */
    MyMenuFragment myMenuFragment;
    /**
     * 速查 碎片
     */
    QuickSearchMenuFragment quickSearchMenuFragment;
    /**
     * 我的个人中心碎片
     */
    MyUserInfoFragment myUserInfoFragment;
    /**
     * 我的积分碎片
     */
    MyIntegralFragment myIntegralFragment;
    /**
     * 积分详情碎片
     */
    IntegralDetailsFragment integralDetailsFragment;
    /**
     * 积分类型碎片
     */
    IntegralTypeListFragment integralTypeListFragment;
    /**
     * 积分规则碎片
     */
    IntegralRuleFragment integralRuleFragment;
    /**
     * 我的轨迹碎片
     */
    MyLocusFragment myLocusFragment;
    /**
     * 积分抽奖碎片
     */
    IntegralLotteryFragment integralLotteryFragment;
    /**
     * 软件设置碎片
     */
    SetUpFragment setUpFragment;
    /**
     * 我的经验值
     */
    MyExperienceFragment myExperienceFragment;
    /**
     * 我的经验头衔
     */
    MyExperienceTitleFragment myExperienceTitleFragment;
    /**
     * 大师之路
     */
    MasterFragment masterFragment;
    /**
     * 大师之旅
     */
    MastersTourFragment mastersTourFragment;
    /**
     * 闯关之旅
     */
    CheckpointsTripFragment checkpointsTripFragment;
    /**
     * 课件目录
     */
    CourseCatalogFragment courseCatalogFragment;
    /**
     * 打开考试
     */
    OnlineExamFragment onlineExamFragment;

    /**
     * 打开ppt课件
     */
    ImageListCoursewareDetailFragment imageListCoursewareDetailFragment;
    /**
     * 我的收藏
     */
    MyCollectFragment myCollectFragment;
    /**
     * 修改密码
     */
    UpdatePasswordFragment updatePasswordFragment;
    /**
     * 意见反馈 (建议或意见)
     */
    SuggestFragment suggestFragment;
    /**
     * 版本信息
     */
    VersionFragment versionFragment;
    /**
     * 检测版本
     */
    CheckingVersionUpdateFragment checkingVersionUpdateFragment;
    /**
     * 关于软件
     */
    AboutFragment aboutFragment;
    /**
     * html5页面
     */
    Html5Fragment html5Fragment;


    /**
     * 当前currFragment
     */
    BaseFragment currFragment;


    ConfirmCancelAskDialog versionConfirmCancelAskDialog;

    View lastView;
    @ViewById
    TextView footerHomeText;
    @ViewById
    View footerLayout;

    String appverUrl;

    @Bean(CheckingVersionServiceImpl.class)
    CheckingVersionService checkingVersionService;

    @Bean(GameRecentServiceImpl.class)
    GameRecentService gameRecentService;

    public HomeFragment getHomeFragment() {
        return homeFragment;
    }

    public void setHomeFragment(HomeFragment homeFragment) {
        this.homeFragment = homeFragment;
    }

    @AfterViews
    public void intiView() {
        checkingVersionService.init(this);
        gameRecentService.init(this);
        // footerHomeClick();
        footerClick(footerHomeText);

//        VersionService_.intent(this).start();


        checkingVersionService.checkingVersion();
//        openIntegralLotteryFragment();

        //启动线程，轮询获取赛果
        timer.schedule(task, 1000, TIME); // 1s后执行task,经过1s再次执行

    }
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                gameRecentService.gameRecent();
            }
            super.handleMessage(msg);
        };
    };
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            // 需要做的事:发送消息
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };
    private int TIME = 10 * 1000;

    /**
     * 底部首页点击
     */
    public void footerHomeClick() {
        if (homeFragment == null) {
            homeFragment = HomeFragment_.builder().build();
        }
        showCurrFragment(homeFragment);
    }

    /**
     * 底部学习点击
     */
    public void footerTrainClick() {
        if (learnFragment == null) {
            learnFragment = LearnMenuFragment_.builder().build();
        }
        showCurrFragment(learnFragment, true);
    }

    /**
     * 底部考试点击
     */
    public void footerExamClick() {
        if (examMenuFragment == null) {
            examMenuFragment = ExamMenuFragment_.builder().build();
        }
        showCurrFragment(examMenuFragment, true);
    }

    /**
     * 底部我的个人中心点击
     */
    public void footerMyClick() {
        if (myMenuFragment == null) {
            myMenuFragment = MyMenuFragment_.builder().build();
        }
        showCurrFragment(myMenuFragment);
    }

    /**
     * 底部速查点击
     */
    public void footerQuickSearchClick() {
        if (quickSearchMenuFragment == null) {
            quickSearchMenuFragment = QuickSearchMenuFragment_.builder()
                    .build();
        }
        showCurrFragment(quickSearchMenuFragment);
    }

    /**
     * 打开学习列表碎片
     */
    public void openLearnListFragment() {
        if (learnListFragment == null) {
            learnListFragment = LearnListFragment_.builder().build();

        }
        showCurrFragment(learnListFragment, true);
    }

    /**
     * 打开试卷目录碎片
     */
    public void openPaperCatalogFragment() {
        if (paperCatalogFragment == null) {
            paperCatalogFragment = PaperCatalogFragment_.builder().build();

        }
        showCurrFragment(paperCatalogFragment, true);
    }

    /**
     * 打开我的个人中心页面
     */
    public void openMyUserInfoFragment() {
        if (myUserInfoFragment == null) {
            myUserInfoFragment = MyUserInfoFragment_.builder().build();

        }
        showCurrFragment(myUserInfoFragment, true);
    }

    /**
     * 打开我的积分
     */
    public void openMyIntegralFragment() {
        if (myIntegralFragment == null) {
            myIntegralFragment = MyIntegralFragment_.builder().build();
        }
        showCurrFragment(myIntegralFragment, true);
    }

    /**
     * 我的经验值
     */
    public void openMyExperienceFragment() {
        if (myExperienceFragment == null) {
            myExperienceFragment = MyExperienceFragment_.builder().build();
        }
        showCurrFragment(myExperienceFragment, true);
    }

    /**
     * 打开积分详情碎片
     *
     * @param titile 标题
     * @param type   0 积分详情 1 经验详情
     */
    public void openIntegralDetailsFragment(String titile, int type) {
        if (integralDetailsFragment == null) {
            integralDetailsFragment = IntegralDetailsFragment_.builder()
                    .build();
        }
        integralDetailsFragment.setTitle(titile);
        integralDetailsFragment.setType(type);
        showCurrFragment(integralDetailsFragment, true);
    }

    /**
     * 打开积分类型碎片
     */
    public void openIntegralTypeListFragment() {
        if (integralTypeListFragment == null) {
            integralTypeListFragment = IntegralTypeListFragment_.builder()
                    .build();
        }
        showCurrFragment(integralTypeListFragment, true);
    }

    /**
     * 打开积分规则碎片
     */
    public void openIntegralRuleFragment() {
        if (integralRuleFragment == null) {
            integralRuleFragment = IntegralRuleFragment_.builder().build();
        }
        showCurrFragment(integralRuleFragment, true);
    }

    /**
     * 打开我的轨迹
     */
    public void openMyLocusFragment() {
        if (myLocusFragment == null) {
            myLocusFragment = MyLocusFragment_.builder().build();
        }
        showCurrFragment(myLocusFragment, true);
    }

    /**
     * 打开积分抽奖页面
     */
    public void openIntegralLotteryFragment() {
        if (integralLotteryFragment == null) {
            integralLotteryFragment = IntegralLotteryFragment_.builder()
                    .build();
        }
        showCurrFragment(integralLotteryFragment, true);
    }

    /**
     * 打开经验列表
     */
    public void openMyExperienceTitleFragment(int experience) {
        if (myExperienceTitleFragment == null) {
            myExperienceTitleFragment = MyExperienceTitleFragment_.builder()
                    .build();
        }
        myExperienceTitleFragment.setExperience(experience);
        showCurrFragment(myExperienceTitleFragment, true);
    }

    /**
     * 打开设置碎片
     */
    public void openSetUpFragment() {
        if (setUpFragment == null) {
            setUpFragment = SetUpFragment_.builder().build();
        }
        showCurrFragment(setUpFragment, true);
    }

    /**
     * 打开大师之路
     */
    public void openMasterFragment() {
        if (masterFragment == null) {
            masterFragment = MasterFragment_.builder().build();
        }
        showCurrFragment(masterFragment, true);
    }

    /**
     * 打开大师闯关之旅
     */
    public void openMastersTourFragment() {
        if (mastersTourFragment == null) {
            mastersTourFragment = MastersTourFragment_.builder().build();
        }
        showCurrFragment(mastersTourFragment, true);
    }


    /**
     * 打开闯关之旅
     */
    public void openCheckpointsTripFragment() {
        if (checkpointsTripFragment == null) {
            checkpointsTripFragment = CheckpointsTripFragment_.builder().build();
        }
        showCurrFragment(checkpointsTripFragment, true);

    }

    /**
     * 课程目录
     */

    public void openCourseCatalogFragment() {
        if (courseCatalogFragment == null) {
            courseCatalogFragment = CourseCatalogFragment_.builder().build();
        }
        showCurrFragment(courseCatalogFragment, true);
    }

    /**
     * 打开考试
     */
    public void openOnlineExamFragment() {
//        if (onlineExamFragment == null) {
        onlineExamFragment = OnlineExamFragment_.builder().build();
//        }
        showCurrFragment(onlineExamFragment, true);
    }

    /**
     * 打开ppt课件
     */
    public void openImageListCoursewareDetailFragment() {
//        if (imageListCoursewareDetailFragment == null) {
        imageListCoursewareDetailFragment = ImageListCoursewareDetailFragment_.builder().build();
//        }
        showCurrFragment(imageListCoursewareDetailFragment, true);
    }

    /**
     * 打开我的收藏
     */
    public void openMyCollectFragment() {
        if (myCollectFragment == null) {
            myCollectFragment = MyCollectFragment_.builder().build();
        }
        showCurrFragment(myCollectFragment, true);
    }


    /**
     * 打开修改个人资料
     *
     * @param title
     * @param value
     * @param position
     * @param currUserInfoJsonArray
     */
    public void openUpdateUserInfoFragment(String title, String value, int position, JSONArray currUserInfoJsonArray) {
        UpdateUserInfoFragment updateUserInfoFragment = null;
        if (updateUserInfoFragment == null) {
            updateUserInfoFragment = UpdateUserInfoFragment_.builder().build();
        }
        updateUserInfoFragment.setTitle(title);
        updateUserInfoFragment.setValue(value);
        updateUserInfoFragment.setPosition(position);
        updateUserInfoFragment.setCurrUserInfoJsonArray(currUserInfoJsonArray);
        showCurrFragment(updateUserInfoFragment, true);
    }

    /**
     * 修改密码
     */
    public void openUpdatePasswordFragment() {
        if (updatePasswordFragment == null) {
            updatePasswordFragment = UpdatePasswordFragment_.builder().build();
        }
        showCurrFragment(updatePasswordFragment, true);
    }

    /**
     * 打开意见反馈 (建议或意见)
     */
    public void openSuggestFragment() {
        if (suggestFragment == null) {
            suggestFragment = SuggestFragment_.builder().build();
        }
        showCurrFragment(suggestFragment, true);
    }

    /**
     * 打开版本信息
     */
    public void openVersionFragment() {
        if (versionFragment == null) {
            versionFragment = VersionFragment_.builder().build();
        }
        showCurrFragment(versionFragment, true);
    }

    /**
     * 打开检测版本
     */
    public void openCheckingVersionUpdateFragment() {
        if (checkingVersionUpdateFragment == null) {
            checkingVersionUpdateFragment = CheckingVersionUpdateFragment_.builder().build();
        }

        showCurrFragment(checkingVersionUpdateFragment, true);
    }


    /**
     * 关于软件
     */
    public void openAboutFragment() {
        if (aboutFragment == null) {
            aboutFragment = AboutFragment_.builder().build();
        }
        showCurrFragment(aboutFragment, true);
    }

    /**
     * 打开帮助信息
     */
    public void openHelpFragment() {
        openHtml5Fragment("帮助信息", "http://www.borgwardapp.com/help/help.html");
    }

    /**
     * 打开帮助信息
     */
    public void openHtml5Fragment(String title, String url) {
        if (html5Fragment == null) {
            html5Fragment = Html5Fragment_.builder().build();
        }
        html5Fragment.setTitile(title);
        html5Fragment.setContentUrl(url);
        showCurrFragment(html5Fragment, true);
    }

    /**
     * 限时挑战
     */
    public void openLimitedChallengeFragment() {
        if(limitedChallengeFragment==null){
            limitedChallengeFragment= LimitedChallengeFragment_.builder().build();
        }
        showCurrFragment(limitedChallengeFragment, true);
    }

    public void openPKFragment (){
        if(pkFragment==null){
            pkFragment= PKFragment_.builder().build();
        }
        showCurrFragment(pkFragment, true);
    }

    @Override
    public void showCurrFragment(Fragment fragment, boolean isAddBack) {
        super.showCurrFragment(fragment, isAddBack);
        if (fragment instanceof BaseFragment) {
            this.currFragment = (BaseFragment) fragment;
        }
    }

    /**
     * 得到当前Fragment
     *
     * @return
     */
    public BaseFragment getCurrFragment() {
        if (currFragment == null)
            try {
                List<Fragment> fragments = getSupportFragmentManager().getFragments();
                for (int i = fragments.size() - 1; i >= 0; i--) {
                    Fragment fragment = fragments.get(i);
                    if (fragment != null && fragment instanceof BaseFragment && fragment.isVisible()) {
                        currFragment = (BaseFragment) fragment;
                        return currFragment;
                    }
                }
            } catch (Exception ex) {
                ErrorUtils.outErrorLog(ex);
            }
        return currFragment;

    }

    @Override
    public void onBackPressed() {
        if (getCurrFragment() != null) {
            if (!getCurrFragment().backPressed()) {
                return;
            }
        }

        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            super.onBackPressed();
        } else {
            getSupportFragmentManager().popBackStack();
            currFragment = null;
        }
//        super.onBackPressed();
    }

    /**
     * 进入闯关之旅
     */
    public void enterCheckpointsTripActivity(View view) {
        try {
            setIntentInt("i", Integer.parseInt(String.valueOf(view.getTag())));
        } catch (Exception e) {
            setIntentInt("i", 1);
        }
        openMastersTourFragment();
    }


    /**
     * 底部点击
     *
     * @param view
     */
    public void footerClick(View view) {
        if (lastView != null && view != lastView) {
            lastView.setSelected(false);

        }

        view.setSelected(true);
        lastView = view;
        reflexRunClick(view);
    }

    /**
     * 隐藏底部
     */
    public void hideFooterLayout() {
        footerLayout.setVisibility(View.GONE);
    }

    /**
     * 隐藏底部
     */
    public void showFooterLayout() {
        footerLayout.setVisibility(View.VISIBLE);
    }

    /**
     * Fragment点击
     */
    public void reflexRunFragmentClick(String runMethod) {
        if (getCurrFragment() != null) {
            getCurrFragment().runMethod(runMethod);
        } else {
            reflexRunClick(runMethod);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (getCurrFragment() == imageListCoursewareDetailFragment) {
            imageListCoursewareDetailFragment.onConfigurationChanged(newConfig);
        }
        super.onConfigurationChanged(newConfig);
    }

    @UiThread
    @Override
    public void checkingVersionSuccess(JSONObject jsonObject) {
        JSONObject dataJsonObject = TypeUtils.getJsonObject(jsonObject, "data");
        String version = TypeUtils.getJsonString(dataJsonObject, "version");
        appverUrl = TypeUtils.getJsonString(dataJsonObject, "package");
        if (!BuildConfig.VERSION_NAME.equals(version)) {
            //更新
            if (versionConfirmCancelAskDialog == null) {
                versionConfirmCancelAskDialog = new ConfirmCancelAskDialog(this, false);
            }
            versionConfirmCancelAskDialog.show();
            versionConfirmCancelAskDialog.setTitleAndroidContent("版本更新", "宝沃学院有新版本，是否安装？");
            versionConfirmCancelAskDialog.setRunMethod("updateVersionClick");
            versionConfirmCancelAskDialog.setCancelRunMethod("cancelVersionClick");
        }
    }

    /**
     * 更新版本
     */
    public void updateVersionClick() {
        Intent exitIntent = new Intent();
        exitIntent.setAction(Constants.EXIT_ACTIVITYS_ACTION);
        sendBroadcast(exitIntent);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(appverUrl));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    /**
     * 取消更新
     */
    public void cancelVersionClick() {
        Intent exitIntent = new Intent();
        exitIntent.setAction(Constants.EXIT_ACTIVITYS_ACTION);
        sendBroadcast(exitIntent);
        finish();
    }

    @UiThread
    @Override
    public void checkingVersionError(String error) {
    }

    @Override
    protected void onDestroy() {
        if (versionConfirmCancelAskDialog != null && versionConfirmCancelAskDialog.isShowing()) {
            versionConfirmCancelAskDialog.cancel();
        }
        currFragment = null;
        if(timer != null){
            timer.cancel();
        }
        super.onDestroy();
    }


    public void openPKRankFragment(String name,String avatarUrl,int jifeng,int winrate) {
        if(pkRankFragment==null){
            pkRankFragment= PKRankFragment_.builder().build();
        }
        pkRankFragment.setName(name);
        pkRankFragment.setAvatarUrl(avatarUrl);
        pkRankFragment.setJifengNumber(jifeng);
        pkRankFragment.setWinRate(winrate);
        showCurrFragment(pkRankFragment, true);
    }

    public void openPKGameCategoryFragment(String name, String avatar,
                                           int quarter, int rate,
                                           int paperCateId) {
        if(pkGameCategoryFragment==null){
            pkGameCategoryFragment= PKGameCategoryFragment_.builder().build();
        }
        pkGameCategoryFragment.setName(name);
        pkGameCategoryFragment.setAvatarUrl(avatar);
        pkGameCategoryFragment.setJifengNumber(quarter);
        pkGameCategoryFragment.setWinRate(rate);
        pkGameCategoryFragment.setPaperCateId(paperCateId);
        showCurrFragment(pkGameCategoryFragment, true);
    }

    public void openPKHallFragment(int gameCategoryId,int playerLimit) {
        if(pkHallFragment==null){
            pkHallFragment= PKHallFragment_.builder().build();
        }
        pkHallFragment.setGameCategoryId(gameCategoryId);
        pkHallFragment.setPlayerLimit(playerLimit);
        showCurrFragment(pkHallFragment, true);
    }

    public void openPKConfirmFragment(int roomId,int seatId,int playerLimit,PKHallFragment fragment) {
        if(pkConfirmFragment==null){
            pkConfirmFragment= PKConfirmFragment_.builder().build();
        }
        pkConfirmFragment.setRoomId(roomId);
        pkConfirmFragment.setSeatId(seatId);
        pkConfirmFragment.setPkHallFragment(fragment);
        pkConfirmFragment.setPlayerLimit(playerLimit);
        showCurrFragment(pkConfirmFragment, true);
    }

    public void openPKMorePersonHallFragment(int gameCategoryId) {
        if(pkMorePersonHallFragment==null){
            pkMorePersonHallFragment= PKMorePersonHallFragment_.builder().build();
        }
        pkMorePersonHallFragment.setGameCategoryId(gameCategoryId);
        showCurrFragment(pkMorePersonHallFragment, true);
    }

    public void openPKMorePersonConfirmFragment(int gameId,int roomId,int remaining_time) {
        if(pkMorePersonConfirmFragment==null){
            pkMorePersonConfirmFragment= PKMorePersonConfirmFragment_.builder().build();
        }
        pkMorePersonConfirmFragment.setGameId(gameId);
        pkMorePersonConfirmFragment.setRoomId(roomId);
        pkMorePersonConfirmFragment.setRemainingTime(remaining_time);
        //pkMorePersonConfirmFragment.setSeatId(seatId);
        showCurrFragment(pkMorePersonConfirmFragment, true);
    }

    public void openMyExamFragment() {
        if(myExamFragment==null){
            myExamFragment= MyExamFragment_.builder().build();
        }
        showCurrFragment(myExamFragment, true);
    }

    public void openBuyCalculatorFragment() {
        if(buyCalculatorFragment==null){
            buyCalculatorFragment= BuyCalculatorActivity_.builder().build();
        }
        showCurrFragment(buyCalculatorFragment, true);
    }

    public void openCarTypeDetailFragment(int categoryId,String cateName) {
        if(carTypeDetailFragment==null){
            carTypeDetailFragment= CarTypeDetailActivity_.builder().build();
        }
        carTypeDetailFragment.setCategoryId(categoryId);
        carTypeDetailFragment.setCategoryName(cateName);
        showCurrFragment(carTypeDetailFragment, true);
    }

    public void openPriceFragment(CarDetailBean carDetailBean,
                                  ArrayList<DownPaymentBean> downPaymentBeanList
            ,ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList,String categoryName) {
        if(priceFragment==null){
            priceFragment= PriceFragment_.builder().build();
        }
        //priceFragment.setCarDetailBean(carDetailBean);
        PriceFragment.carDetailBean = carDetailBean;
        priceFragment.setDownPaymentBeanList(downPaymentBeanList);
        priceFragment.setRePaymentPeriodBeanList(rePaymentPeriodBeanList);
        priceFragment.setCategoryName(categoryName);
        showCurrFragment(priceFragment, true);
    }

    public void openMustPayFragment(CarDetailBean carDetailBean) {
        if(mustPayFragment==null){
            mustPayFragment= MustPayFragment_.builder().build();
        }
        mustPayFragment.setCarDetailBean(carDetailBean);
        showCurrFragment(mustPayFragment, true);
    }

    public void openShangYeBaoXianFragment(CarDetailBean carDetailBean) {
        if(shangYeBaoXianFragment==null){
            shangYeBaoXianFragment= ShangYeBaoXianFragment_.builder().build();
        }
        //shangYeBaoXianFragment.setCarDetailBean(carDetailBean);
        showCurrFragment(shangYeBaoXianFragment, true);
    }

    public void openDownPaymentsPercentFragment(CarDetailBean carDetailBean
            ,ArrayList<DownPaymentBean> downPaymentBeanList ) {
        if(downPaymentsPercentFragment==null){
            downPaymentsPercentFragment= DownPaymentsPercentFragment_.builder().build();
        }
        downPaymentsPercentFragment.setCarDetailBean(carDetailBean);
        downPaymentsPercentFragment.setDownPaymentBeanList(downPaymentBeanList);
        showCurrFragment(downPaymentsPercentFragment, true);
    }

    public void openRepaymentPeriodFragment(CarDetailBean carDetailBean
            ,ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList) {
        if(repaymentPeriodFragment==null){
            repaymentPeriodFragment= RepaymentPeriodFragment_.builder().build();
        }
        repaymentPeriodFragment.setCarDetailBean(carDetailBean);
        repaymentPeriodFragment.setRePaymentPeriodBeanList(rePaymentPeriodBeanList);
        showCurrFragment(repaymentPeriodFragment, true);
    }

    public void openDiSanZheZeRenXianFragment(CarDetailBean carDetailBean) {
        if(diSanZheZeRenXianFragment==null){
            diSanZheZeRenXianFragment= DiSanZheZeRenXianFragment_.builder().build();
        }
        //diSanZheZeRenXianFragment.setCarDetailBean(carDetailBean);
        showCurrFragment(diSanZheZeRenXianFragment, true);
    }

    public void openBoLiDanDuPoSuiXianFragment(CarDetailBean carDetailBean) {
        if(boLiDanDuPoSuiXianFragment==null){
            boLiDanDuPoSuiXianFragment= BoLiDanDuPoSuiXianFragment_.builder().build();
        }
        //boLiDanDuPoSuiXianFragment.setCarDetailBean(carDetailBean);
        showCurrFragment(boLiDanDuPoSuiXianFragment, true);
    }

    public void openCheShengHuaHengXianFragment(CarDetailBean carDetailBean) {
        if(cheShengHuaHengXianFragment==null){
            cheShengHuaHengXianFragment= CheShengHuaHengXianFragment_.builder().build();
        }
        //cheShengHuaHengXianFragment.setCarDetailBean(carDetailBean);
        showCurrFragment(cheShengHuaHengXianFragment, true);
    }

    public void refreshUI() {
        if(priceFragment != null){
            priceFragment.refreshUI();
        }
    }

    @UiThread
    @Override
    public void gameRecentError(String serverConnectionError) {
        Toast.makeText(this,serverConnectionError,Toast.LENGTH_SHORT).show();
    }

    @UiThread
    @Override
    public void gameRecentSuccess(JSONObject jsonObject) {
        JSONArray dataList = TypeUtils.getJsonArray(jsonObject,"data","games");
        if (dataList != null) {
            ArrayList<GameResultBean> gameResultBeanArrayList = new ArrayList<>();
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject playerJsonObject = dataList.getJSONObject(i);
                GameResultBean bean = new GameResultBean();
                bean.setId(playerJsonObject.getInt("id"));
                bean.setScore(playerJsonObject.getInt("score"));
                bean.setResult(playerJsonObject.getBoolean("result"));
                bean.setCateId(playerJsonObject.getInt("cateId"));
                bean.setCateName(playerJsonObject.getString("cateName"));
                bean.setRuleName(playerJsonObject.getString("ruleName"));

                gameResultBeanArrayList.add(bean);

                new PKSaiResultDialog(HomeActivity.this,bean).show();

            }

        }

    }

    public void openPKResultFragment(int gameId) {
        if(pkResultFragment==null){
            pkResultFragment= PKResultFragment_.builder().build();
        }
        pkResultFragment.setGameId(gameId);
        showCurrFragment(pkResultFragment, true);
    }
}
